package com.baizhi.service.impl;

import com.baizhi.annotation.DeleteAnnotation;
import com.baizhi.annotation.InsertAnnotation;
import com.baizhi.service.UserService;
import com.baizhi.service.UserThreadServcie;
import com.baizhi.util.ThreadPollUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Service
public class UserThreadServcieImpl implements UserThreadServcie {

    private Logger logger= LoggerFactory.getLogger(UserThreadServcieImpl.class);
    //自动赋值
    @Autowired(required = false)
    private UserService userService;

    @Override
    @InsertAnnotation
    public Map selectAllCountThread() throws InterruptedException {
        long startTime=System.currentTimeMillis();
    //创建一个map
        Map map=new HashMap();
        //创建一个计数器，给值3
        CountDownLatch countDownLatch = new CountDownLatch(3);

        //开启要优化的三个子线程
        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                //查询男女比例
                try {
                    List<Map> maps = userService.selectSex();
                    map.put("sex",maps);
                    //计数器减一-1
                    countDownLatch.countDown();
                }catch (Exception e){
                    countDownLatch.countDown();
                }

            }
        });

        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {

                try {
                    //查询注册量
                Map map1=userService.selectDate();
                map.put("count",map1);
                countDownLatch.countDown();
                }catch (Exception e){
                    countDownLatch.countDown();
                }
            }
        });

        ThreadPollUtil.executor.execute(new Runnable() {
            @Override
            public void run() {
                List<Map> maps = userService.selectCity();
                map.put("province",maps);
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        long endTime=System.currentTimeMillis();
        logger.debug("优化后的时间"+(endTime-startTime)+"ms");
        return map;
    }

    @Override
    @InsertAnnotation
    public Map selectAllCountFuture() throws ExecutionException, InterruptedException {
        Map map = new HashMap<>();
        Future<String> future1 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                long startTime=System.currentTimeMillis();
                Map map1 = userService.selectDate();
                map.put("data",map1);
//                计数器减一
                return "haha";
            }
        });

        Future<String> future2 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = userService.selectSex();
                map.put("sex",maps);
                return "haha";
            }
        });

        Future<String> future3 = ThreadPollUtil.executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                List<Map> maps = userService.selectCity();
                map.put("province",maps);
                return "haha";
            }
        });

        if ("haha".equals(future1.get()) && "haha".equals(future2.get()) && "haha".equals(future3.get())){
            return map;
        }

        return map;
    }

    @Override
    //没有优化
    @InsertAnnotation
    public Map selectAllCount() {
        long startTime=System.currentTimeMillis();
        Map map=new HashMap();
        //注册量
        Map map1=userService.selectDate();
        //地区分布
        List<Map> maps = userService.selectCity();
        //男女分布
        List<Map> maps1 = userService.selectSex();
        map.put("sex",maps1);
        map.put("data",map1);
        map.put("province",maps);
        long endTime=System.currentTimeMillis();
        logger.debug("没有优化"+(endTime-startTime)+"ms");
        return map;
    }
}
